import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

public class shiLi7 {
    public static void main(String[] args) {
        try {
            showPhoneInfo();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void showPhoneInfo() throws IOException {
        Document doc = null;
        try{
            //加载DOM树
            SAXReader saxReader =  new SAXReader();
            doc = saxReader.read(new File("src/shiLi2.xml"));

        } catch (DocumentException e) {
            e.printStackTrace();
        }
        //获取XML的根节点
        Element root  = doc.getRootElement();

        //遍历所有的brand标签
        for (Iterator itBrand = root.elementIterator(); itBrand.hasNext(); ) {
            Element brand = (Element) itBrand.next();
            //输出标签的name属性
            System.out.println("品牌："+brand.attributeValue("name"));
            for (Iterator itType = brand.elementIterator(); itType.hasNext(); ) {
                //输出标签的name属性
                Element type = (Element) itType.next();
                System.out.println("\t型号："+type.attributeValue("name"));
            }
        }
//        Element test = root.addElement("test");
//        test.addAttribute("name","OPOP");
//        root.add(test);
//        XMLWriter writer = new XMLWriter(new FileWriter("src/shiLi66.xml"));
//        writer.write(doc);
//        writer.close();


    }
}
